package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.LoginBean;
import com.example.administrator.liveapp.bean.RegisteredYzPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;
import com.google.gson.Gson;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信第三方登录activity
 */
public class WxLoginPage extends Activity implements View.OnClickListener{


    private SharedPrefsUtil sp;
    private LoginBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wx_login_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
    }

    private void initView() {
        findViewById(R.id.weixin).setOnClickListener(this);
        findViewById(R.id.code_login).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.weixin:
                UMShareAPI.get(this).getPlatformInfo(this, SHARE_MEDIA.WEIXIN, weiXinUmAuthListener);
                break;
            case R.id.code_login:
                startActivity(new Intent(this,LoginPage.class));
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                break;
        }
    }

    /**
     * 微信授权
     */
    private UMAuthListener weiXinUmAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Toast.makeText(WxLoginPage.this, "微信正在授权，请稍后...", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onComplete(SHARE_MEDIA share_media, int i,
                               final Map<String, String> map) {
            Log.e("666666", map + "---" + map.get("openid"));
            map.put("tid", "1");
            String url = AppUrl.URL + AppUrl.LoginURL;
            XUtils.Get(url, map, new MyCallBack<String>() {
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String message = jsonObject.optString("message");
                    String results = jsonObject.optString("result");
                    if (results.equals("true")) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, LoginBean.class);
                        sp.putStringValue(WxLoginPage.this, "openid",map.get("openid"));
                        sp.putStringValue(WxLoginPage.this, "unionid",map.get("unionid"));
                        sp.putStringValue(WxLoginPage.this, "screen_name",map.get("screen_name"));
                        sp.putStringValue(WxLoginPage.this, "iconurl",map.get("iconurl"));
                        ApplicationUtils.getInstance().loginBean = bean;
                        Intent intent = new Intent();
                        intent.setClass(WxLoginPage.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                        Toast.makeText(WxLoginPage.this, "授权成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(WxLoginPage.this, message + "登录失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    super.onError(ex, isOnCallback);
                    Toast.makeText(WxLoginPage.this, ex.getMessage()+ "登录失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFinished() {
                    super.onFinished();
                }
            });
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            Toast.makeText(WxLoginPage.this, "授权失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {
            Toast.makeText(WxLoginPage.this, "授权取消", Toast.LENGTH_SHORT).show();
        }
    };

}
